package cl.nfa;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class State {
    
    private Map<Character, List<State>> connections;
    private boolean terminating;
    
    public State(boolean terminating) {
        this.terminating = terminating;
        
        connections = new HashMap<Character, List<State>>();
    }
    
    public void addConnection(char value, State node) {
        if(!connections.containsKey(value)) {
            connections.put(value, new ArrayList<State>());
        }
        
        connections.get(value).add(node);
    }
    
    public void setTerminating(boolean value) {
        terminating = value;
    }
    
    public boolean isTerminating() {
        return terminating;
    }
    
    public List<State> getConnections(char value) {
        List<State> states = new ArrayList<State>();
        
        if(connections.containsKey(value)) {
            states.addAll(connections.get(value));
        }
        
        if(connections.containsKey(NFA.EPSILON)) {
            states.addAll(connections.get((char)0));
        }
        
        return states;
    }
    
    public String toString() {
        String result = "";
        
        for(char c : connections.keySet()) {
            result += c == NFA.EPSILON? "epsilon: " : c + ": ";
            
            for(State s : connections.get(c)) {
                result += s.hashCode() + ", ";
            }
            result += "\n";
        }
        
        return result;
    }
}
